//
//  ZLAlert+ZLButton.m
//  ZLAlert-OC
//
//  Created by 科大 on 2024/1/2.
//

#import "ZLAlert+ZLButton.h"
@implementation ZLAlertButton
+ (ZLAlertButton *)zl_buttonWithTitle:(NSString *)title
                              dismiss:(BOOL)dismiss
                               config:(ZLAlertItemConfig *)config
                               action:(nullable ZlAlertAction)action {
    ZLAlertButton *button = [ZLAlertButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = config.cornerRadius;
    button.layer.borderWidth = config.borderWidth;
    button.layer.borderColor = config.borderColor.CGColor;
    button.layer.masksToBounds = YES;
    [button setTitleColor:config.color forState:UIControlStateNormal];
    button.titleLabel.font = config.font;
    button.titleLabel.textAlignment = config.alignment;
    button.backgroundColor = config.backgroundColor;
    [button setTitle:title forState:UIControlStateNormal];
    button.dismiss = dismiss;
    button.action = action;
    
    switch (config.alignment) {
        case NSTextAlignmentLeft:
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            break;
        case NSTextAlignmentRight:
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            break;
        default:
            break;
    }
    
    return button;
}
@end

@implementation ZLAlertDoubleButton


- (instancetype)initWithLeftTitle:(NSString *)leftTitle
                      leftDismiss:(BOOL)leftDismiss
                       leftConfig:(ZLAlertItemConfig *)leftConfig
                       leftAction:(nullable ZlAlertAction)leftAction
                   showCenterLine:(BOOL)showCenterLine
                       lineConfig:(ZLAlertItemConfig *)lineConfig
                       rightTitle:(NSString *)rightTitle
                     rightDismiss:(BOOL)rightDismiss
                      rightConfig:(ZLAlertItemConfig *)rightConfig
                      rightAction:(nullable ZlAlertAction)rightAction {
    self = [super initWithFrame:CGRectZero];
    if (self) {
        _leftButton = [ZLAlertButton zl_buttonWithTitle:leftTitle
                                                dismiss:leftDismiss
                                                 config:leftConfig
                                                 action:leftAction];
        [_leftButton addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftButton];
        [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.equalTo(self).offset(leftConfig.insets.left);
            make.right.equalTo(self.mas_centerX).offset(-leftConfig.insets.right);
        }];
        
        _rightButton = [ZLAlertButton zl_buttonWithTitle:rightTitle
                                                 dismiss:rightDismiss
                                                  config:rightConfig
                                                  action:rightAction];
        [_rightButton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightButton];
        [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.equalTo(self.mas_centerX).offset(rightConfig.insets.left);
            make.right.equalTo(self).offset(-rightConfig.insets.right);
        }];
        
        if (!showCenterLine) {
            return self;
        }
        
        _centerLine = [UIView new];
        _centerLine.backgroundColor = lineConfig.backgroundColor;
        [self addSubview:_centerLine];
        [_centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.top.bottom.equalTo(self);
            make.width.equalTo(@(lineConfig.height));
        }];
    }
    return self;
}

- (void)leftAction {
    [self.delegate alertDoubleButtonTapLeft:self.leftButton];
}

- (void)rightAction {
    [self.delegate alertDoubleButtonTapRight:self.rightButton];
}


@end

@implementation ZLAlert (ZLButton)

- (ZLAlert *)addDoubleAction:(NSString *)leftTitle
                  leftAction:(nullable ZlAlertAction)leftAction
              showCenterLine:(BOOL)showCenterLine
                  rightTitle:(NSString *)rightTitle
                 rightAction:(nullable ZlAlertAction)rightAction {
    return [self addDoubleAction:leftTitle
                     leftDismiss:YES
                      leftConfig:ZLAlertConfig.shared.cancelButtonConfig
                      leftAction:leftAction
                  showCenterLine:showCenterLine
                      lineConfig:ZLAlertConfig.shared.lineConfig
                      rightTitle:rightTitle
                    rightDismiss:YES
                     rightConfig:ZLAlertConfig.shared.submitButtonConfig
                     rightAction:rightAction];
}

- (ZLAlert *)addDoubleAction:(NSString *)leftTitle
                 leftDismiss:(BOOL)leftDismiss
                  leftConfig:(ZLAlertItemConfig *)leftConfig
                  leftAction:(nullable ZlAlertAction)leftAction
              showCenterLine:(BOOL)showCenterLine
                  lineConfig:(ZLAlertItemConfig *)lineConfig
                  rightTitle:(NSString *)rightTitle
                rightDismiss:(BOOL)rightDismiss
                 rightConfig:(ZLAlertItemConfig *)rightConfig
                 rightAction:(nullable ZlAlertAction)rightAction {
    UIView *lastView = self.mainView.subviews.lastObject;
    ZLAlertDoubleButton *doubleButton = [[ZLAlertDoubleButton alloc] initWithLeftTitle:leftTitle
                                                                           leftDismiss:leftDismiss
                                                                            leftConfig:leftConfig
                                                                            leftAction:leftAction
                                                                        showCenterLine:showCenterLine
                                                                            lineConfig:lineConfig
                                                                            rightTitle:rightTitle
                                                                          rightDismiss:rightDismiss
                                                                           rightConfig:rightConfig
                                                                           rightAction:rightAction];
    doubleButton.delegate = self;
    [self.mainView addSubview:doubleButton];
    
    [doubleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.mainView);
        make.top.equalTo(lastView ? lastView.mas_bottom : self.mainView);
        make.height.mas_equalTo(MAX(leftConfig.height, rightConfig.height));
    }];
    
    return [self layoutMainView:0];
}

- (ZLAlert *)addButton:(NSString *)title action:(nullable ZlAlertAction)action {
    return [self addButton:title
                   dismiss:YES
                    config:ZLAlertConfig.shared.submitButtonConfig
                    action:action];
}

- (ZLAlert *)addButton:(NSString *)title
               dismiss:(BOOL)dismiss
                config:(ZLAlertItemConfig *)config
                action:(nullable ZlAlertAction)action {
    UIView *lastView = self.mainView.subviews.lastObject;
    ZLAlertButton *button = [self makeButton:title
                                     dismiss:dismiss
                                      config:config
                                      action:action];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainView).offset(config.insets.left);
        make.right.equalTo(self.mainView).offset(-config.insets.right);
        make.top.equalTo(lastView ? lastView.mas_bottom : self.mainView).offset(config.insets.top);
        make.height.mas_equalTo(config.height);
    }];
    
    return [self layoutMainView:0];
}

- (ZLAlertButton *)makeButton:(NSString *)title
                      dismiss:(BOOL)dismiss
                       config:(ZLAlertItemConfig *)config
                       action:(nullable ZlAlertAction)action {
    ZLAlertButton *button = [ZLAlertButton zl_buttonWithTitle:title
                                                      dismiss:dismiss
                                                       config:config
                                                       action:action];
    [button addTarget:self action:@selector(zl_actionWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:button];
    return button;
}

- (void)alertDoubleButtonTapLeft:(ZLAlertButton *)button {
    [self zl_actionWithButton:button];
}

- (void)alertDoubleButtonTapRight:(ZLAlertButton *)button {
    [self zl_actionWithButton:button];
}

- (void)zl_actionWithButton:(ZLAlertButton *)button {
    if (button.dismiss) {
        [self dismissViewControllerAnimated:YES completion:^{
            if (button.action) {
                button.action();
            }
        }];
        return;
    }
    
    if (button.action) {
        button.action();
    }
}

@end
